package com.useeinfo.oa.modules.maintaining.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.security.entity.SystemUser;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @auther: bin.liu
 * @Date: 2019/1/9 10:24
 * @Description: 维保工单日志
 */
@Entity
@Table(name = "maintain_work_log")
public class MaintainWorkLog extends SuperClassEntity<MaintainWorkLog> {

    /**
     * 流水号
     */
    private String serial;

    /**
     * 业务数据编号
     */
    private String code;

    /**
     * 日志分类
     */
    @ManyToOne
    @JoinColumn(name = "log_cat")
    private DictInfo logCat;

    /**
     * 重要程度
     */
    @ManyToOne
    @JoinColumn(name = "log_level")
    private DictInfo logLevel;

    /**
     * 工作内容
     */
    @Column(length = 500)
    private String content;

    /**
     * 日志照片
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "maintain_work_log_photo", joinColumns = {
            @JoinColumn(name = "log_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> logPhoto = new HashSet<Resource>();

    /**
     * 关联工单
     */
    @ManyToOne
    @JoinColumn(name = "maintain_order_id")
    private MaintainOrder maintainOrder;

    /**
     * 申请人
     */
    @ManyToOne
    @JoinColumn(name = "user_id")
    private SystemUser systemUser;

    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public DictInfo getLogCat() {
        return logCat;
    }

    public void setLogCat(DictInfo logCat) {
        this.logCat = logCat;
    }

    public DictInfo getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(DictInfo logLevel) {
        this.logLevel = logLevel;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Set<Resource> getLogPhoto() {
        return logPhoto;
    }

    public void setLogPhoto(Set<Resource> logPhoto) {
        this.logPhoto = logPhoto;
    }

    public MaintainOrder getMaintainOrder() {
        return maintainOrder;
    }

    public void setMaintainOrder(MaintainOrder maintainOrder) {
        this.maintainOrder = maintainOrder;
    }

    public SystemUser getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();

        if (getLogPhoto() != null) {
            JSONArray jsonArray = new JSONArray();
            for (Resource resource : getLogPhoto()) {
                jsonArray.add(resource.getPath());
            }
            jsonObject.put("logPhoto", jsonArray);
        }

        if (getLogPhoto() != null) {
            JSONArray jsonArray = new JSONArray();
            JSONObject json;
            for (Resource resource : getLogPhoto()) {
                json = new JSONObject();
                json.put("id",resource.getId());
                json.put("imgPath",StringConverters.null2String(resource.getPath()));
                jsonArray.add(json);
            }
            jsonObject.put("logPhotoArray", jsonArray);
        }

        jsonObject.put("serial", StringConverters.null2String(getSerial()));
        jsonObject.put("code", StringConverters.null2String(getCode()));
        jsonObject.put("logCatId", getLogCat() == null ? "" : StringConverters.null2String(getLogCat().getId()));
        jsonObject.put("logCatName", getLogCat() == null ? "" : StringConverters.null2String(getLogCat().getPropertyName()));
        jsonObject.put("logLevelId", getLogLevel() == null ? "" : StringConverters.null2String(getLogLevel().getId()));
        jsonObject.put("logLevelName", getLogLevel() == null ? "" : StringConverters.null2String(getLogLevel().getPropertyName()));
        jsonObject.put("userName", StringConverters.null2String(getSystemUser().getUserName()));
        jsonObject.put("workContent", StringConverters.null2String(getContent()));
        jsonObject.put("workLogLevelId", getLogLevel() == null ? "" : StringConverters.null2String(getLogLevel().getId()));
        jsonObject.put("workLogCatId", getLogCat() == null ? "" : StringConverters.null2String(getLogCat().getId()));

        return jsonObject;
    }

    public JSONObject toJSONObjectDetail() {
        JSONObject jsonObject = super.toJSONObject();
        if (getLogPhoto() != null) {
            JSONArray jsonArray = new JSONArray();
            for (Resource resource : getLogPhoto()) {
                jsonArray.add(resource.getPath());
            }
            jsonObject.put("logPhoto", jsonArray);
        }
        if (getLogPhoto() != null) {
            JSONArray jsonArray = new JSONArray();
            JSONObject json;
            for (Resource resource : getLogPhoto()) {
                json = new JSONObject();
                json.put("id",resource.getId());
                json.put("imgPath",StringConverters.null2String(resource.getPath()));
                jsonArray.add(json);
            }
            jsonObject.put("logPhotoArray", jsonArray);
        }

        jsonObject.put("serial", StringConverters.null2String(getSerial()));
        jsonObject.put("code", StringConverters.null2String(getCode()));
        jsonObject.put("logCatId", getLogCat() == null ? "" : getLogCat().getId());
        jsonObject.put("logCatName", getLogCat() == null ? "" : StringConverters.null2String(getLogCat().getPropertyName()));
        jsonObject.put("logLevelId", getLogLevel() == null ? "" : getLogLevel().getId());
        jsonObject.put("logLevelName", getLogLevel() == null ? "" : StringConverters.null2String(getLogLevel().getPropertyName()));
        jsonObject.put("userName", StringConverters.null2String(getSystemUser().getUserName()));
        jsonObject.put("workContent", StringConverters.null2String(getContent()));
        jsonObject.put("workLogLevelId", getLogLevel() == null ? "" : StringConverters.null2String(getLogLevel().getId()));
        jsonObject.put("workLogCatId", getLogCat() == null ? "" : StringConverters.null2String(getLogCat().getId()));

        return jsonObject;
    }
}
